<?php
/*
*All rights reserved: Json.
*Links:http://www.liyaoquan.cn.
*Links:http://imarkchina.cn.
*Links:http://www.imarkchina.cn.
*/

/**
 * 博客后台日志模型，负责数据，增，删，改，查
 * 因为没有博客数据没使用数据库，所以没有继承 Model 类
 */
Class GetpostModel extends PublicModel{

	/**
	 * 全部日志数据处理
	 * @param  boolean $check [description]
	 * @return [type]         [description]
	 */
	Public function showPost($check = false){
		if ($check) {
			$temp = loadfile(C('post.postid').$check.'.php');
			$pageurl = U('index', array('lid' => $check), true);
		} else {
			$temp = $this->AllPostData();
			$pageurl = U('index', '', true);
		}
		$indexpage = 12;
		$count = count($temp);
		$page = new Page($count, $indexpage, $pageurl);
		$pnum = explode(',', $page->limit());
		$data = array();
		$i = -1;
		foreach ($temp as $k => $v) {
			$i++;
			if ($i < $pnum[0] || $i >= $pnum[0] + $pnum[1]) continue;
			$data[] = unfile(C('post.postdata').$k.'.Mark');
		}
		$data = $this->RgetunFile($data);
		$topfile = loadfile(C('top'));
		foreach ($data as $k => $v) {
			if (in_array_case($v['id'], $topfile)) {
				$data[$k]['istop'] = 1;
			} else {
				$data[$k]['istop'] = 0;
			}
		}
		$redata['list'] = $this->getListData();
		$redata['post'] = $data;
		$redata['page'] = $page->view();
		return $redata;
	}

	/**
	 * 取出日志添加页中的分类
	 */
	Public function GPD(){
		return $this->getListData();
	}

	/**
	 * 写入日志数据
	 * @param [type] $data [description]
	 */
	Public function Add($data){
		if (empty($data['id'])) {
			$data['id'] = randCode(6);
			if (file_exists(C('post.postdata').$data['id'].'.Mark')) {
				$data['id'] = randCode(6);
			}
		}
		if (empty($data['date']) && empty($data['time'])) {
			$date = date('Y.m.d');
			$time = date('H:i:s', time());
		} else {
			$date = $data['date'];
			$time = $data['time'];
		}
		$listfile = loadfile(C('post.listid'));
		$data['list'] = $listfile[$data['listid']]['title'];
		$data['date'] = $date;
		$data['time'] = $time;
		$data['content'] = $_POST['content'];
		$data['sdate'] = substr($data['date'], 0, 7);
		$str = Getstr(htmlspecialchars_decode($data['content']), 300);
		$data['des'] = strip_tags($str);
		if (empty($data['tags'])) {
			$data['tags'] = explode('，', trim(Getstr($data['title'], 8)));
		} else {
			$data['tags'] = explode('，', trim($data['tags']));
		}
		$data['keyword'] = '';
		foreach ($data['tags'] as $v) {
			$data['keyword'] .= $v.',';
		}
		$data['keyword'] = substr($data['keyword'], 0, -1);
		$data['isoff'] = 1;
		preg_match('/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $data['content'], $match);
		if (!empty($match)) {
			$data['imagesurl'] = $match[0];
		}
		$dir = C('post.postid').$data['listid'].'.php';
		$file = loadfile($dir);
		$listidfile[$data['id']] = array(
			'date' => $data['date'],
			'time' => $data['time'],
			);
		$lastfileput = array_merge((array)$file, (array)$listidfile);
		uasort($lastfileput, 'Post_Sort');
		$config = $this->blogConfig();
		if (!empty($config['baidu_token_key'])) {
			if (URLREWRITE) {
				$purl = U().'/id'.C('URLSPACE').$data['id'].'.html';
				$turl = array();
				foreach ($data['tags'] as $v) {
					$turls = U().'/s'.C('URLSPACE').$v.'.html';
					$turl[] = $turls;
				}
			} else {
				$purl = U('index/show/index', array('id' => $data['id']));
				$turl = array();
				foreach ($data['tags'] as $v) {
					$turls = U('index/search/s', array('s' => $v));
					$turl[] = $turls;
				}
			}
			$purl = array($purl);
			$urls = array_merge($purl, $turl);
			$this->BaiDuPost($urls, $config['baidu_token_key']);
		}
		$hitsfiles = loadfile(C('hits'));
		$gethitarray = array();
		$gethitarray[$data['id']] = 100;
		$lasthits = array_merge($gethitarray, $hitsfiles);
		uasort($lasthits, 'hits_sort');
		varfile(C('hits'), $lasthits);
		if (varfile($dir, $lastfileput)) {
			if (serfile(C('post.postdata').$data['id'].'.Mark', $data)) {
				return true;
			}
		}
	}

	/**日志置顶首页数据处理
	 * [IsTOP description]
	 */
	Public function IsTOP(){
		$file = loadfile(C('top'));
		$data = array();
		foreach ($file as $k => $v) {
			$data[] = unfile(C('post.postdata').$k.'.Mark');
		}
		return $this->RgetunFile($data);
	}

	/**
	 * 处理设置置顶日志操作
	 * @param boolean $id [description]
	 */
	Public function SetIsTOP($id = false, $lid = false){
		$file = loadfile(C('top'));
		if (count($file) >= 10) {
			return false;
		}
		$lidfile = loadfile(C('post.postid').$lid.'.php');
		$temp = array();
		$temp[$id] = $lidfile[$id];
		$array = array_merge($file, $temp);
		uasort($array, 'Post_Sort');
		if (varfile(C('top'), $array)) {
			return true;
		}
	}

	/**
	 * 处理取消置顶日志操作
	 * @param boolean $id [description]
	 */
	Public function DelIsTOP($id = false){
		$file = loadfile(C('top'));
		unset($file[$id]);
		uasort($file, 'Post_Sort');
		if (varfile(C('top'), $file)) {
			return true;
		}
	}

	/**
	 * 回收站数据处理
	 * @param boolean $check [description]
	 */
	Public function RecoveryData($check = false){
		if ($check) {
			$temp = loadfile(C('post.postrecovery').$check.'.php');
			$pageurl = U('recindex', array('lid' => $check), true);
		} else {
			$temp = $this->AllPostData(true);
			$pageurl = U('recindex', '', true);
		}
		$indexpage = 12;
		$count = count($temp);
		$page = new Page($count, $indexpage, $pageurl);
		$pnum = explode(',', $page->limit());
		$data = array();
		$i = -1;
		foreach ($temp as $k => $v) {
			$i++;
			if ($i < $pnum[0] || $i >= $pnum[0] + $pnum[1]) continue;
			$data[] = unfile(C('post.postdata').$k.'.Mark');
		}

		$redata['list'] = $this->getListData();
		$redata['post'] = $this->RgetunFile($data);
		$redata['page'] = $page->view();
		return $redata;
	}

	/**
	 * 回收日志数据处理
	 * @return [type] [description]
	 */
	Public function rec(){
		$lid = G('get.lid');
		$id = G('get.id');
		$topfile = loadfile(C('top'));
		if (in_array_case($id, $topfile)) {
			unset($topfile[$id]);
			uasort($topfile, 'Post_Sort');
			varfile(C('top'), $topfile);
		}
		$hits = loadfile(C('hits'));
		unset($hits[$id]);
		uasort($hits, 'hits_sort');
		varfile(C('hits'), $hits);
		$file = loadfile(C('post.postid').$lid.'.php');
		if (!file_exists(C('post.postrecovery').$lid.'.php')) {
			varfile(C('post.postrecovery').$lid.'.php', array());
		}
		$refile = loadfile(C('post.postrecovery').$lid.'.php');
		$recovery[$id] = $file[$id];
		$arraydata = array_merge($refile, $recovery);
		uasort($arraydata, 'Post_Sort');
		unset($file[$id]);
		uasort($file, 'Post_Sort');
		if (varfile(C('post.postrecovery').$lid.'.php', $arraydata) && varfile(C('post.postid').$lid.'.php', $file)) {
			return true;
		}
	}

	/**
	 * 回收站日志还原
	 */
	Public function PostRePlay(){
		$lid = G('get.lid');
		$id = G('get.id');
		$file = loadfile(C('post.postid').$lid.'.php');
		$refile = loadfile(C('post.postrecovery').$lid.'.php');
		$rearray[$id] = $refile[$id];
		$arraydata = array_merge($file, $rearray);
		uasort($arraydata, 'Post_Sort');
		unset($refile[$id]);
		uasort($refile, 'Post_Sort');
		if (varfile(C('post.postrecovery').$lid.'.php', $refile) && varfile(C('post.postid').$lid.'.php', $arraydata)) {
			return true;
		}
	}

	/**
	 * 批量回收日志数据处理
	 * @param boolean $lid [description]
	 * @param boolean $id  [description]
	 */
	Public function AllRe($lid = false, $id = false){
		if (!file_exists(C('post.postrecovery').$lid.'.php')) {
			varfile(C('post.postrecovery').$lid.'.php', array());
		}
		$refile = loadfile(C('post.postrecovery').$lid.'.php');
		$file = loadfile(C('post.postid').$lid.'.php');
		$temp = array();
		foreach ($id as $v) {
			foreach ($file as $k => $va) {
				if ($v == $k) {
					$temp[$v] = $va;
					unset($file[$v]);
				}
			}
		}
		uasort($file, 'Post_Sort');
		$lastarr = array_merge($refile, $temp);
		uasort($lastarr, 'Post_Sort');
		if (varfile(C('post.postrecovery').$lid.'.php', $lastarr) && varfile(C('post.postid').$lid.'.php', $file)) {
			return true;
		}
	}

	/**
	 * 批量还原日志与删除数据处理
	 * @param boolean $check [description]
	 * @param boolean $data  [description]
	 */
	Public function Allrep($check = false, $data = false){
		$lid = $data['lid'];
		$refile = loadfile(C('post.postrecovery').$lid.'.php');
		$temp = array();
		if ($check) {
			$lidfile = loadfile(C('post.postid').$lid.'.php');
			foreach ($data['id'] as $v) {
				foreach ($refile as $k => $va) {
					if ($v == $k) {
						$temp[$v] = $va;
						unset($refile[$v]);
					}
				}
			}
			uasort($refile, 'Post_Sort');
			$lastarr = array_merge($lidfile, $temp);
			uasort($lastarr, 'Post_Sort');
			if (varfile(C('post.postrecovery').$lid.'.php', $refile) && varfile(C('post.postid').$lid.'.php', $lastarr)) {
				return true;
			}
		} else {
			foreach ($data['id'] as $v) {
				unset($refile[$v]);
				unlink(C('post.postdata').$v.'.php');
				$pluanfileid = loadfile(C('pinglun.id').$v.'.php');
				if (!empty($pluanfileid)) {
					foreach ($pluanfileid as $k => $v) {
						unlink(C('pinglun.data').$k.'.Mark');
					}
				}
				unlink(C('pinglun.id').$v.'.php');
			}
			uasort($refile, 'Post_Sort');
			if (varfile(C('post.postrecovery').$lid.'.php', $refile)) {
				return true;
			}
		}
	}

	/**
	 * 回收站日志彻底删除
	 */
	Public function PostDel(){
		$lid = G('get.lid');
		$id = G('get.id');
		$file = loadfile(C('post.postrecovery').$lid.'.php');
		unset($file[$id]);
		uasort($file, 'Post_Sort');
		$pluanfileid = loadfile(C('pinglun.id').$id.'.php');
		if (!empty($pluanfileid)) {
			foreach ($pluanfileid as $k => $v) {
				unlink(C('pinglun.data').$k.'.Mark');
			}
		}
		if (varfile(C('post.postrecovery').$lid.'.php', $file) && unlink(C('post.postdata').$id.'.Mark')) {
			$delpfile = C('pinglun.id').$id.'.php';
			if (file_exists($delpfile)) {
				unlink($delpfile);
			}
			return true;
		}
	}
}